package com.codeczx.popupwindowdemo;

import android.app.ActionBar;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private int x;
    private int y;
    private static final String TAG = "MainActivity";
    private long startTime = 0;
    public static final int LONG_CLICK_INTERVAL = 1000;
    private boolean mShow = false;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:

            }
        }
    };
    PopupWindow popupWindow;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View contentView = getLayoutInflater().inflate(R.layout.popup_content, null);
        // 必须设置大小，不然popupWindow不显示，也可以通过setWidth和setHeight来设置
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        // 点击外部区域后关闭popupwindow
        popupWindow.setFocusable(true);
        // 据说要设置这个才能点击外部区域后关闭popupwindow，但是实测不用
//        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));

        button = (Button) findViewById(R.id.buttonPanel);
        final TextView textView = (TextView) popupWindow.getContentView().findViewById(R.id.text);
//        button.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                popupWindow.showAsDropDown(button, -x, -y);
//                return true;
//            }
//        });
        button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i(TAG, "onTouch: "+event.getAction());
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mHandler.postDelayed(,1000);
                        break;
                    case MotionEvent.ACTION_MOVE:

                        break;
                }
                return true;
            }
        });
//        textView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                textView.setText("PopupWindowDemo click");
//                popupWindow.dismiss();
//            }
//        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mShow = false;
            }
        });

    }

    private class MyRunnable implements Runnable{

        @Override
        public void run() {
            popupWindow.showAsDropDown(button, -x, -y);
        }
    }
}
